package com.anycl.design.behavioral.nullobject.instance;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Slf4j
public class Main {
    public static void main(String[] args) {
        // 具体对象
        User concreteUser = new User(new Address(new Country("china")));
        // 空对象
        User nullUser = new User(null);
        // 具体对象编码获取
        String concreteCode = Optional.of(concreteUser)
                .flatMap(User::getAddress)
                .flatMap(Address::getCountry)
                .map(Country::getCode)
                .orElse("null").toUpperCase();

        String nullCode = Optional.of(nullUser)
                .flatMap(User::getAddress)
                .flatMap(Address::getCountry)
                .map(Country::getCode)
                .orElse("null").toUpperCase();
        log.info(concreteCode);
        log.info(nullCode);
    }
}
